﻿using PhotoGallery.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Hosting;
using System.Web.Mvc;
using PagedList;
using PhotoGallery.Models.ViewModels;
using PhotoGallery.Models.Utils;
using System.IO;



namespace PhotoGallery.Controllers
{
    [Authorize]
    public class AlbumsController : BaseController
    {
        //
        // GET: /Albums/
        public ActionResult Index()
        {
            return RedirectToAction("List");
        }

        public ActionResult Details(int? id)
        {
            if (!id.HasValue)
            {
                return RedirectToAction("Index");
            }
            AlbumViewModel viewModel = new AlbumViewModel();
            viewModel.Album = new AlbumsModel().Get(id ?? default(int));
            return View(viewModel);
        }

        //
        // GET: /Album/Create

        public ActionResult Create()
        {
            if (Request.UrlReferrer != null)
            {
                TempData["backUrl"] = Request.UrlReferrer.ToString();
                ViewBag.backUrl = Request.UrlReferrer.ToString();
            }
            else
            {
                ViewBag.backUrl = Url.Action("Index");
            }
            return View(new AlbumViewModel());
        }

        
        [HttpPost]
        public ActionResult Create(AlbumViewModel viewModel, HttpPostedFileBase[] files)
        {
            ViewBag.backUrl = TempData["backUrl"];
            TempData.Keep();
            if (ModelState.IsValid)
            {
                AlbumsModel model = new AlbumsModel();
                try
                {
                    int albumID = model.Create(viewModel.Album, files[0]);

                    AlbumInfo album = new AlbumsModel().Get(albumID);
                    PhotosModel photo = new PhotosModel(album);
                    foreach (HttpPostedFileBase file in files)
                    {
                        if (FileValidation.IsValid(file,new List<string>(){
                            ".jpg",".jpeg",".png",".bmp","tif"
                        }))
                        {
                            photo.Create(new PhotoInfo(), file);
                        }
                    }

                    return RedirectToAction("Index");
                }
                catch
                {
                    return View(viewModel);
                }
            }
            return View(viewModel);
        }

        //
        // GET: /Album/Edit/5

        public ActionResult Edit(int? id)
        {
            if (!id.HasValue)
            {
                return RedirectToAction("Index");
            }
            if (Request.UrlReferrer != null)
            {
                TempData["backUrl"] = Request.UrlReferrer.ToString();
                ViewBag.backUrl = Request.UrlReferrer.ToString();
            }
            else
            {
                ViewBag.backUrl = Url.Action("Index");
            }
            AlbumViewModel viewModel = new AlbumViewModel();
            viewModel.Album = new AlbumsModel().Get(id ?? default(int));
            return View(viewModel);
        }

        //
        // POST: /Album/Edit/5

        [HttpPost]
        public ActionResult Edit(AlbumViewModel viewModel, HttpPostedFileBase file)
        {
            ViewBag.backUrl = TempData["backUrl"];
            TempData.Keep();
            if (ModelState.IsValid)
            {
                AlbumsModel model = new AlbumsModel();
                try
                {
                    if (FileValidation.IsValid(file, new List<string>(){
                            ".jpg",".jpeg",".png",".bmp","tif"
                    }))
                    {
                        model.Update(viewModel.Album, file);
                    }
                    return Redirect(ViewBag.backUrl);
                }
                catch
                {
                    return View(viewModel);
                }
            }

            return View(viewModel);
        }

        //
        // GET: /Album/Delete/5

        public ActionResult Delete(int? id)
        {
            if (!id.HasValue)
            {
                return RedirectToAction("Index");
            }
            TempData["backUrl"] = Request.UrlReferrer.ToString();
            ViewBag.backUrl = Request.UrlReferrer.ToString();

            AlbumInfo album = new AlbumsModel().Get(id ?? default(int));
            return View(album);
        }

        //
        // POST: /Album/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, AlbumInfo album)
        {
            ViewBag.backUrl = TempData["backUrl"];
            TempData.Keep();
            AlbumsModel model = new AlbumsModel();
            try
            {
                model.Delete(album);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ViewBag.ErrorMsg = "Error deleting record. " + ex.Message;
                return View(album);
            }
        }

        public ActionResult List(int? page)
        {
            AlbumsModel model = new AlbumsModel();
            var albums = model.Get();

            var pageNumber = page ?? 1;
            var onePageOfProducts = albums.ToPagedList(pageNumber, 12);

            ViewBag.OnePageOfProducts = onePageOfProducts;
            return View(model.Get());
        }
        public ActionResult Download(string file = "")
        {

            var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            var mimeType = "image/jpeg";
            var fileDownloadName = Path.GetFileName(file);
            return File(fileStream, mimeType, fileDownloadName);
        }
    }
}
